package com.iltgcl.echovoicedemo.ui.base

import android.app.Application
import androidx.lifecycle.*
import kotlinx.coroutines.Dispatchers

/**
 * @Author leicao
 * @Date 20-9-23 上午10:17
 */
abstract class BaseViewModel(application: Application) : AndroidViewModel(application) {

    private val _snackbarText = MutableLiveData<Event<Int>>()
    val snackbarText: LiveData<Event<Int>> = _snackbarText

    fun showSnackbarMessage(message: Int) {
        _snackbarText.value = Event(message)
    }

    inline fun <T> launchOnViewModelScope(crossinline block: suspend () -> LiveData<T>): LiveData<T> {
        return liveData(viewModelScope.coroutineContext + Dispatchers.IO) {
            emitSource(block())
        }
    }
}